/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package battleship;
import javax.swing.JOptionPane;
import java.util.Random;
import java.util.StringTokenizer;

/**
 *
 * @author Shaun Pickford
 */
public class Turn {

    //OpponentGameBoard opponent = new OpponentGameBoard();
    char[] opponentBoard = Main.gameBoard.getOpponentGameBoard();
    char[] playerBoard = Main.gameBoard.getPlayerGameBoard();
    Conversion conversionObj = new Conversion();

    public void Turn()
    {
    }

    /* Starts turn by getting input for the turn */
    public int startTurn(char who)
    {
        int shot = -1;
        if(who == 'p')
        {
            /* Get input from human player and convert to numerical coordinate */
            while(shot == -1)
            {
                //JOptionPane.showMessageDialog(null, "Your Turn!");
                InputCoordinate getShot = new InputCoordinate();
                shot = conversionObj.toNumber(getShot.promptForCoordinate());
            }
        }//if players turn
        if(who == 'o')
        {
            /* This will generate a number between 1 and 99...needs to be increased
             * by 1 to be a valid 1-100 number.
             */
            //Random rand = new Random();
            //shot = rand.nextInt(100) + 1;
            //AI AI = new AI();

            boolean validShot = false;
            while(!validShot)
            {
                shot = Main.aiObject.takeShot();
                if(shot > 0 && shot <= 100)
                {
                    validShot = true;
                }
            }
            
        }
        return shot;


    }

    public void takeShot(int userMove, char who){

        boolean sunk = false;
        if(who == 'p')
        {

        /* Test to see if move is a hit or miss */
        String hitOrMiss;
        String tempHitOrMiss = Main.gameBoard.hitOrMiss(userMove, who);
        StringTokenizer st = new StringTokenizer(tempHitOrMiss);
        hitOrMiss = st.nextToken("-");
        //System.out.println("hitOrMiss is " + hitOrMiss);
        if(st.hasMoreTokens())
        {
            String strSunk = st.nextToken();
            System.out.println(strSunk);
            if(strSunk.equals("sunk"))
            {
                sunk = true;
            }
        }

        if(hitOrMiss.equals("miss"))
        {
            JOptionPane.showMessageDialog(null, "You Missed!");
            Main.gameBoard.playerPreviousShot = userMove;
            Main.gameBoard.playerPreviousShotResult = "Miss";
            Main.whosTurn = 'o';
        }

        else if(hitOrMiss.equals("tryAgain"))
        {
            JOptionPane.showMessageDialog(null, "Please try again!");
            Main.whosTurn = 'p';
        }
        else if(sunk)
        {
            JOptionPane.showMessageDialog(null, "You Hit and Sunk the Opponents " + hitOrMiss + "!");
            Main.playerHits = Main.playerHits + 1;
            Main.whosTurn = 'o';
            Main.gameBoard.playerPreviousShot = userMove;
            Main.gameBoard.playerPreviousShotResult = "Hit and sink";
            //System.out.println("Player hits: " + Main.playerHits);
        }
        else
        {
            JOptionPane.showMessageDialog(null, "You Hit the Opponents " + hitOrMiss + "!");
            Main.playerHits = Main.playerHits + 1;
            Main.whosTurn = 'o';
            Main.gameBoard.playerPreviousShot = userMove;
            Main.gameBoard.playerPreviousShotResult = "Hit";
            //System.out.println("Player hits: " + Main.playerHits);
        }

        }//if players turn


        
        /* If opponents turn */
        if(who == 'o')
        {
             /* Test to see if move is a hit or miss */
            String hitOrMiss;
            String tempHitOrMiss = Main.gameBoard.hitOrMiss(userMove, who);
            StringTokenizer st = new StringTokenizer(tempHitOrMiss);
            hitOrMiss = st.nextToken("-");
            if(st.hasMoreTokens())
            {
                String strSunk = st.nextToken();
                //System.out.println(strSunk);
                if(strSunk.equals("sunk"))
                {
                    sunk = true;
                }
            }

           
            if(hitOrMiss.equals("miss"))
            {
                JOptionPane.showMessageDialog(null, "Opponent shoots at " + conversionObj.toCoordinate(userMove) + "!");
                JOptionPane.showMessageDialog(null, "They Missed!");
                Main.gameBoard.opponentPreviousShot = userMove;
                Main.gameBoard.opponentPreviousShotResult = "Miss";
                Main.whosTurn = 'p';
            }

            else if(hitOrMiss.equals("tryAgain"))
            {
                //JOptionPane.showMessageDialog(null, "Please try again!");
                Main.whosTurn = 'o';
                Main.gameBoard.opponentPreviousShotResult = "tryAgain";
            }
            else if(sunk)
            {
              JOptionPane.showMessageDialog(null, "Opponent shoots at " + conversionObj.toCoordinate(userMove) + "!");
              JOptionPane.showMessageDialog(null, "They Hit and Sunk Your " + hitOrMiss + "!");
              Main.whosTurn = 'p';
              Main.opponentHits = Main.opponentHits + 1;
              Main.gameBoard.opponentPreviousShot = userMove;
            Main.gameBoard.opponentPreviousShotResult = "Hit and sink";
              //System.out.println("Opponent hits: " + Main.opponentHits);
            }

            else
            {
              JOptionPane.showMessageDialog(null, "Opponent shoots at " + conversionObj.toCoordinate(userMove) + "!");
              JOptionPane.showMessageDialog(null, "They Hit Your " + hitOrMiss + "!");
              Main.whosTurn = 'p';
              Main.opponentHits = Main.opponentHits + 1;
              Main.gameBoard.opponentPreviousShot = userMove;
            Main.gameBoard.opponentPreviousShotResult = "Hit";
              //System.out.println("Opponent hits: " + Main.opponentHits);
            }
        }//if opponents turn

    }
}
